﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Class_Entity;
using BussinessObject;
using System.Data;
using System.Data.SqlClient;

public partial class WebApplication_ADEditLiquidFilling : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            loadData();

        }
    }

        private void loadData()

        {
            int id =Convert.ToInt32(Session["ID"].ToString());

            LiquidFilling li = new LiquidFilling();
            BOProducts pro = new BOProducts();
            // fill information into object li
            li.ID = id;
            // fill information of choosen object li into text box
            DataTable dt = new DataTable();
            dt = pro.Find_LiquidFilling(li).Tables[0];
            txtModel.Text = dt.Rows[0]["Model"].ToString();
            txtAirP.Text = dt.Rows[0]["AirPressure"].ToString();
            txtAirV.Text = dt.Rows[0]["AirVolume"].ToString();
            txtFillS.Text = dt.Rows[0]["FillingSpeed"].ToString();
            txtFillR.Text = dt.Rows[0]["FillingRange"].ToString();
        
        
        }




        protected void btSubmit_Click(object sender, EventArgs e)
        {
            LiquidFilling li = new LiquidFilling();
            BOProducts pro = new BOProducts();

            // fill information into object li
            int id = Convert.ToInt32(Session["ID"].ToString());
            li.ID = id;

            DataTable dt = new DataTable();
            dt = pro.Find_LiquidFilling(li).Tables[0];

            
            li.Model = txtModel.Text.Trim();
            li.AirPressure = Convert.ToInt32(txtAirP.Text.Trim());
            li.AirVolume = Convert.ToInt32(txtAirV.Text.Trim());
            li.FillingSpeed = Convert.ToInt32(txtFillS.Text.Trim());            
            li.FillingRangen = Convert.ToInt32(txtFillR.Text.Trim());
            // if filename not change then use ole file name
            if (FileUploadImage.FileName.Equals(""))
            {
                li.Image = dt.Rows[0]["Image"].ToString();
            }
            // filename changed then use and upload new image file
            else
            {
                li.Image = "~\\WebApplication\\images\\" + FileUploadImage.FileName;
                FileUploadImage.SaveAs(Server.MapPath("~\\WebApplication\\images\\" + FileUploadImage.FileName));
            }

            // check if validation are true then add to database
            if (Session["a"] == null)
            {
                pro.Edit_LiquidFilling(li);
                HttpContext.Current.Response.Write("<script type='text/javascript'>alert('" + "Edit Successfully ..." + "');window.location.href='ADLiquidFilling.aspx';</script>");


            }
        }
        // check if model name exits or not

        protected void txtModel_TextChanged(object sender, EventArgs e)
        {
            LiquidFilling li = new LiquidFilling();
            BOProducts bo = new BOProducts();
            DataSet ds = bo.Load_LiquidFilling(li);
            DataTable dt = ds.Tables[0];
            Label1.Text = "";
            Session["a"] = null;

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (txtModel.Text.Trim() == dt.Rows[i]["Model"].ToString())
                {
                    Session["a"] = 1;
                    Label1.Text = "Model already exits";
                }

            }
        }
}